import $api from '@/config/baseRequest'
import jweixin from '@/config/jssdk'
export default class h5Share {
	constructor(imgUrl, shopName, link) {
		this.imgUrl = imgUrl;
		this.shopName = shopName;
		this.link = link;
		this.getWxSDK();
	}

	getWxSDK() {
		var uri = encodeURIComponent(location.href.split("#")[0]);
		$api.get('/addons/yun_shop/api.php?i=4&uuid=0&type=1&shop_id=null&scope=pass&route=member.member.wxJsSdkConfig&url='+uri).then(res => {
			console.log(res,123123123123);
			let config = res.data.config
			jweixin.config({
				...res.data.config
			});
			jweixin.ready(() => {
				jweixin.onMenuShareAppMessage({
					title: this.getMessage(location.href).title, // 分享标题
					desc: this.getMessage(location.href).desc, // 分享描述
					link: this.getMessage(location.href).link, // 分享链接
					imgUrl: this.getMessage(location.href).imgUrl, // 分享图标
					type: 'link', // 分享类型,music、video或link，不填默认为link
					dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
					success: function (res) {
						uni.showToast({
							title: '分享成功！'
						})
						console.log(res,'分享成功')
					}
				})
			})
			console.log({
				title: this.getMessage(location.href).title, // 分享标题
				desc: this.getMessage(location.href).desc, // 分享描述
				link: this.getMessage(location.href).link, // 分享链接
				imgUrl: this.getMessage(location.href).imgUrl, // 分享图标
			});
		})
	};
	getMessage(url) {
		let isDetails = url.indexOf('/goods-detail/goods-detail')
		if(isDetails != -1) {
			return {
				title: '【轻奢行】和我一起拼' + this.shopName,
				desc: '拼团红包领不停',
				imgUrl: this.imgUrl,
				link: url
			}
		} else {
			return {
				title: '【轻奢行】和我一起拼团',
				desc: '拼团红包领不停',
				imgUrl: '',
				link: url
			}
		}
	}
}
